//
//  LeftViewController.m
//  LiangYa_BObject
//
//  Created by dllo on 15/10/28.
//  Copyright © 2015年 YuHao. All rights reserved.
//

#import "LeftViewController.h"
#import "LeftTableViewCell.h"
#import "CollectViewController.h"
#import "AppDelegate.h"
#import "ScanViewController.h"

@interface LeftViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableV;

@end

@implementation LeftViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSLog(@"%@", NSHomeDirectory());
        
    self.view.backgroundColor = [UIColor cyanColor];
    UIImageView *imageview = [[UIImageView alloc] initWithFrame:self.view.bounds];
    imageview.image = [UIImage imageNamed:@"leftImage.png"];
//    imageview.contentMode = UIViewAnimationOptionTransitionFlipFromLeft;
    [self.view addSubview:imageview];
    
    self.tableV = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _tableV.delegate = self;
    _tableV.dataSource = self;
    _tableV.scrollEnabled = NO;
    _tableV.separatorStyle = NO;
    [self.view addSubview:_tableV];
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        CollectViewController *collectVC = [[CollectViewController alloc] init];
        [self presentViewController:collectVC animated:YES completion:^{
            
        }];
    }else if (indexPath.row == 1){
        [AFRequdstAndLoad clearCaches];
        //刷新tableView
        [self.tableV reloadData];

    }else if (indexPath.row == 3) {
        ScanViewController *scanVC = [[ScanViewController alloc] init];
        [self presentViewController:scanVC animated:YES completion:^{
            
        }];
    }else if (indexPath.row == 4){
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/cn/app/li-chao-jia-zhuang/id1049621592?mt=8"]];

    }
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return Screenheight / 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"leftCell"];
    if (!cell) {
        cell = [[LeftTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"leftCell"];
    }
    
    if (indexPath.row == 0) {
        cell.logo.image = [[UIImage imageNamed:@"shoucang"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.titleL.text = @"收藏";
    }else if (indexPath.row == 1) {
//        cell.clearB.hidden = NO;
        cell.logo.image = [[UIImage imageNamed:@"qingchuhuancun"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        CGFloat cacheSize = [AFRequdstAndLoad getSizeOfCaches];
        NSString *cacheStr = @"";
        if (cacheSize < 1024) {
            cacheStr = [NSString stringWithFormat:@"清除缓存 (%.2fKB)",cacheSize/1024.0];
        } else{
            cacheStr = [NSString stringWithFormat:@"清除缓存 (%.2fM)",cacheSize/1024.0/1024.0];
        }
        cell.titleL.text = cacheStr;
        
        
        
    }else if (indexPath.row == 2) {
        cell.nightS.hidden = NO;
        cell.logo.image = [[UIImage imageNamed:@"yejianmoshi"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        cell.titleL.text = @"夜间模式";
        cell.nightS.on = [[NSUserDefaults standardUserDefaults]boolForKey:@"nightstyle"];
        [cell.nightS addTarget:self action:@selector(nightAction:) forControlEvents:UIControlEventValueChanged];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }else if (indexPath.row == 3){
        cell.logo.image = [[UIImage imageNamed:@"erweima"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.titleL.text = @"二维码扫描";
    }else {
        cell.titleL.text = @"推荐应用";
    }
    
    return cell;
}

- (void)nightAction:(UISwitch *)swi
{
    [[NSUserDefaults standardUserDefaults]setBool:swi.on forKey:@"nightstyle"];
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    
    UIColor *textcolor;
    UIColor *backgroundColor;
    if (swi.on) {
        textcolor = [UIColor whiteColor];
        backgroundColor = [UIColor colorWithRed:57/255.0 green:57/255.0 blue:57/255.0 alpha:1];
        UIView *tabV = [[self.tabBarController.tabBar subviews] firstObject];
        tabV.backgroundColor = [UIColor blackColor];
        self.tabBarController.tabBar.tintColor = [UIColor whiteColor];
    }else{
        textcolor = [UIColor blackColor];
        backgroundColor = [UIColor whiteColor];
        UIView *tabV = [[self.tabBarController.tabBar subviews] firstObject];
        tabV.backgroundColor = [UIColor whiteColor];
        self.tabBarController.tabBar.tintColor = [UIColor redColor];

    }
    
    [center postNotificationName:@"night" object:nil userInfo:@{@"textcolor":textcolor, @"backcolor":backgroundColor}];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
